<template>
     <div class="searchj-box">
        <div class="row">
      <div class="left">
        <el-icon>
          <Search />
        </el-icon>
        <span>筛选搜索</span>
      </div>
      <div class="right">
        <el-button @click="reset">重置</el-button>
        <el-button type="primary" @click="queryUserList">查询搜索</el-button>
      </div>
    </div>
    <el-form>
        <div class="Search-criteria">
        <el-form-item label="优惠券名称" style="padding-right: 30px;">
            <el-input  placeholder="优惠券名称" v-model="queryParams.name" style="width: 200px;" />
        </el-form-item>   
        <el-form-item label="优惠券类型：">
            <el-select  class="m-2" placeholder="全部" v-model="queryParams.type">
    <el-option value="0" label="全场赠券"></el-option>
    <el-option value="1" label="会员赠券"></el-option>
    <el-option value="2" label="购物赠券"></el-option>
    <el-option value="3" label="注册赠券"></el-option>
  </el-select>
        </el-form-item>  
    </div>
    </el-form> 
</div>
    <div class="add-area">
      <div class="text"><el-icon><Tickets /></el-icon><span>数据列表</span></div>
      <el-button type="primary" @click="router.push('/sms/addCoupon')">添加</el-button>
    </div>
<el-table
ref="multipleTableRef"
:data="list"
style="width: 100%"
@selection-change=""
border 
cell-class-name="align-c"
>
<el-table-column type="selection" width="55" />
<el-table-column label="编号" prop="id" align="center"/>
<el-table-column label="优惠券名称" prop="name" />
<el-table-column label="优惠券类型"  prop="type" align="center">
    <template #default="{ row }">
        <div  v-if="row.type===0">
            全场赠券
        </div>
        <div v-else-if="row.type===1">
            会员赠券
        </div>
        <div v-else-if="row.type===2">
            购物赠券
        </div>
        <div v-else-if="row.type===3">
            注册赠券
        </div>
    </template>
</el-table-column>
<el-table-column label="可用商品" prop="useType" align="center">
  <template #default="{ row }">
        <div  v-if="row.useType===0">
            全场通用
        </div>
        <div v-else-if="row.useType===1">
            指定分类
        </div>
        <div v-else-if="row.useType===2">
            指定商品
        </div>
    </template>
</el-table-column>
<el-table-column label="使用门槛" prop="minPoint" align="center">
    <template #default="{ row }">
        满{{ row.minPoint }}元可用
    </template>
</el-table-column>
<el-table-column label="面值" prop="amount" align="center">
    <template #default="{ row }">
        {{ row.amount }}元
    </template>
</el-table-column>
<el-table-column label="使用平台" prop="platform" align="center">
  <template #default="{ row }">
        <div  v-if="row.platform==0">
            全平台
        </div>
        <div v-else-if="row.platform==1">
            移动平台
        </div>
        <div v-else-if="row.platform==2">
            PC平台
        </div>
    </template>
</el-table-column>
<el-table-column label="有效期" align="center" width="180px">
  <template #default="{ row }">
    {{ formateDate(row.startTime) }}至{{ formateDate(row.endTime) }}
  </template>
</el-table-column>
<el-table-column label="状态" align="center" >
    <template  #default="{row}" >
      <div v-if="row.endTime >formateDate(c) ">
        未过期
      </div>
      <div v-else>
        已过期
      </div>
    </template>
</el-table-column>
<el-table-column label="操作" width="200px">
      <template #default="{ row }">
        <el-button text type="primary"  @click="router.push('/sms/couponHistory?id='+row.id)" size="small">查看</el-button>
          <el-button text type="primary" @click="router.push('/sms/updateCoupon?id='+row.id)" size="small">编辑</el-button>
          <el-button text type="primary" @click="del(row.id)"  size="small">删除</el-button>
      </template>
    </el-table-column>
</el-table>
    <ElConfigProvider :locale="zhCn">
      <div class="page">
        <el-pagination v-model:current-page="queryParams.pageNum" v-model:page-size="queryParams.pageSize"
          :page-sizes="[5, 10, 20, 50]" background layout="total, sizes, prev, pager, next, jumper" :total="total"
          @size-change="queryUserList" @current-change="queryUserList" />
      </div>
    </ElConfigProvider>
  </template>
  <script setup lang="ts">
  import { ref, reactive } from "vue";
  import { getCouponList,delCoupon } from "./api";
  import { Search,Tickets } from "@element-plus/icons-vue"
  import { ElConfigProvider, ElMessage, ElMessageBox } from 'element-plus'
  import zhCn from 'element-plus/lib/locale/lang/zh-cn'
  import { formateDate } from '@/utils'
  import { useRouter } from "vue-router";
  
  const router=useRouter()
  const showDialog = ref(false);
  const closeDialog = (isReload?: boolean) => {
  showDialog.value = false;
  if (isReload) queryUserList();
};

const del=(id:number)=>{
    ElMessageBox.confirm("是否要删除该活动?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        delCoupon(id).then((res) => {
          if (res.code === 200) {
            ElMessage.success("删除成功");
            queryUserList()
          }
        });
      })
      .catch(() => {});
  }
  const reset = () => {
      if (queryParams.name?.trim()||queryParams.type?.trim()) {
          queryParams.name = ''
          queryParams.type = ''
          queryUserList()
      }
  }
  const queryParams = reactive<CouponListParams>({
    name:'',
pageNum: 1,
pageSize: 10,
type:'',
  })

    let a = new Date().getTime();
    let b = new Date(a); 
    const c=ref(formateDate(`${b}`))
    // console.log(c);
    
  const list = ref<CouponModel[]>([]);
  const total = ref(0)

  const queryUserList = () => {
    getCouponList(queryParams).then((res) => {
       
      if (res.code === 200) {
        list.value = res.data.list;
        total.value = res.data.total
      }
    });
  };
  queryUserList()
  </script>
  <style lang="less" scoped>
  .searchj-box {
    box-shadow: 0 0 4px #ccc;
    border-radius: 10px;
    padding: 20px;
    margin-bottom: 20px;
    .Search-criteria{
        display: flex;
    }
  
    .row {
      padding: 20px 0;
      display: flex;
  
      &:first-child {
        justify-content: space-between;
      }
  
      &:last-child {
        width: 400px;
  
        span {
          width: 100px;
        }
      }
    }
  }
  
  .page {
    padding: 20px;
    display: flex;
    justify-content: flex-end;
  }
  
  :deep(.align-c) {
    .cell {
      text-align: center !important;
    }
  }
  .add-area {
    display: flex;
    justify-content: space-between;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 0 4px #ccc;
    border-radius: 10px;
    .text{
      display: flex;
      align-items: center;
      span{
        padding-left: 10px;
      }
    }
  }
  </style>
  